﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Blackbird.Core.Editable;
using Blackbird.Core.Editable.Map;
using Blackbird.Core.Editable.Sprites;
using Blackbird.Studio.Context;
using Blackbird.Studio.Mvvm;

namespace Blackbird.Studio.ViewModel.EntityViewModels {
    public class EditableModuleViewModel : EntityViewModel<EditableModule> {
        private EditableMultispriteViewModel _selectedSprite;
        private EditableLayeredMapViewModel _selectedMap;

        [DependentProperty("SelectedSprite")]
        public Visibility SpriteVisibility { get { return SelectedSprite == null ? Visibility.Collapsed : Visibility.Visible; } }

        [DependentProperty("SelectedMap")]
        public Visibility MapVisibility { get { return SelectedMap == null ? Visibility.Collapsed : Visibility.Visible; } }

        public EditableMultispriteViewModel SelectedSprite {
            get { return _selectedSprite; }
            set {
                if (_selectedSprite != value) {
                    _selectedSprite = value;
                    InvokePropertyChanged("SelectedSprite");
                }
            }
        }

        public EditableLayeredMapViewModel SelectedMap {
            get { return _selectedMap; }
            set {
                if (_selectedMap != value) {
                    _selectedMap = value;
                    InvokePropertyChanged("SelectedMap");
                }
            }
        }

        public EditableModuleViewModel(EditableModule model)
            : base(model) {
        }

        public override void Initialize() {
            base.Initialize();

            Sprites = new EditableSpritePackViewModel(new EditableSpritePack());
            Sprites.Initialize();

            Maps = new EditableMapListViewModel(new EditableMapList());
            Maps.Initialize();

            RegisterCommand(AddSpriteCommand = Sprites.AddChildCommand);
            RegisterCommand(AddMapCommand = Maps.AddChildCommand);
        }

        public override string DisplayName {
            get { return Model.Name; }
            set {
                if (Model.Name != value) {
                    Model.Name = value;
                    InvokePropertyChanged("DisplayName");
                }
            }
        }

        public override EntityType EntityType {
            get { return EntityType.Module; }
        }

        public EditableSpritePackViewModel Sprites { get; private set; }
        public EditableMapListViewModel Maps { get; private set; }

        public EntityCommandViewModel AddSpriteCommand { get; private set; }
        public EntityCommandViewModel AddMapCommand { get; private set; }
        
        public override void ReceiveMessage<TSender>(TSender sender, string message) {
            var entity = (sender as IEntityViewModel);
            if (entity != null) {
                switch (entity.EntityType)
                {
                    case EntityType.Module:
                        var moduleVm = (sender as EditableModuleViewModel);
                        break;
                    case EntityType.MultiSprite:
                        var spriteVm = (sender as EditableMultispriteViewModel);
                        if ((message == "IsSelected") && (spriteVm != null))
                        {
                            if (spriteVm.IsSelected)
                            {
                                SelectedSprite = spriteVm;
                                SelectedMap = null;
                            }
                        }
                        break;
                    case EntityType.FramesetList:
                        var framesetListVm = (sender as EditableFramesetListViewModel);
                        break;
                    case EntityType.Frameset:
                        var framesetVm = (sender as EditableFramesetViewModel);
                        break;
                    case EntityType.Frame:
                        var frameVm = (sender as EditableFrameViewModel);
                        break;
                    case EntityType.LayeredMap:
                        var mapVm = (sender as EditableLayeredMapViewModel);
                        if ((message == "IsSelected") && (mapVm != null))
                        {
                            if (mapVm.IsSelected)
                            {
                                SelectedMap = mapVm;
                                SelectedSprite = null;
                            }
                        }
                        break;
                    case EntityType.MapLayer:
                        var layerVm = (sender as EditableMapLayerViewModel);
                        break;
                    case EntityType.MultiFrameMapNode:
                        break;
                    case EntityType.StaticMapNode:
                        break;
                    case EntityType.SpritePack:
                        break;
                    case EntityType.MapList:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }
    }
}